using System;
using System.Collections.Generic;
using System.Text;

namespace ParsingLogic.Types
{
    public class AllActivities
    {
        List<BAMActivity> _activities;
        public List<BAMActivity> BAMActivityList
        {
            get
            {
                if (_activities == null)
                {
                    _activities = new List<BAMActivity>();
                }
                return _activities;
            }
            set
            {
                _activities = value;
            }
        }
        public bool ActivityExists(string ActivityName)
        {
            foreach(BAMActivity act in this.BAMActivityList)
            {
                if(act.Name == ActivityName)
                {
                    return true;
                }

            }
            return false;
        }

        public void Save(string FileName)
        {
           
                System.Xml.Serialization.XmlSerializer objXmlSer = new System.Xml.Serialization.XmlSerializer(typeof(AllActivities));
                System.Xml.XmlTextWriter _metaDataWriter = new System.Xml.XmlTextWriter(FileName, null);
                objXmlSer.Serialize(_metaDataWriter, this);
                _metaDataWriter.Flush();
                _metaDataWriter.Close();
           
        }

        public static AllActivities Load(string FileName)
        {
            if (System.IO.File.Exists(FileName))
            {
                System.Xml.Serialization.XmlSerializer dispXMLSerializer = new System.Xml.Serialization.XmlSerializer(typeof(AllActivities));
                System.Xml.XmlReader _rdr = System.Xml.XmlReader.Create(FileName);
                AllActivities _existingActivities = (AllActivities)dispXMLSerializer.Deserialize(_rdr);
                _rdr.Close();
                return _existingActivities;
            }
            else
            {
                return new AllActivities();
            }
        }
        public void OverwriteBAMActivity(BAMActivity Activity)
        {
            int itemToRemove=0;
            bool found = false;
            for (int i = 0; i < this.BAMActivityList.Count; i++)
            {
                if (this.BAMActivityList[i].Name == Activity.Name)
                {
                    found = true;
                    itemToRemove = i;
                }

            }
            if (found)
            {
                this.BAMActivityList.RemoveAt(itemToRemove);
                this.BAMActivityList.Add(Activity);
            }
        }
    }
}
